#include "Peasant.h"
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
#include <unistd.h>

using namespace std;

typedef struct Infomation
{
    string name;
    int age;
    int score;
    vector<string> loving;
    queue<string> speciality;
} Info;

void Peasant::peasantAddBdosage(Pesticide *pesticide)
{
    this->_pesticide = pesticide;
}

void Peasant::peasantSpray(int num)
{
    this->_pesticide->addBdosage(num);
}

bool Peasant::fire()
{
    this->_pesticide->spray();
    return true;
}

void Peasant::test()
{
    // 结构体
    cout << "结构体:" << endl;
    Info zhangsan = {"zhangsan", 20, 100};
    zhangsan.loving.push_back("跳舞");
    zhangsan.speciality.push("编程");
    cout << "name = " << zhangsan.name << ", "
         << "age = " << to_string(zhangsan.age) << ", "
         << "score = " << to_string(zhangsan.score) << ", "
         << "hobby = " << zhangsan.loving.front() << ", "
         << "speciality = " << zhangsan.speciality.front() << endl;

    // for循环
    cout << "for循环:" << endl;
    int sum = 0;
    for (int i = 1; i < 101; i++)
    {
        sum += i;
    }
    cout << "1到100的和为:" << sum << endl;

    // while循环
    int pro = 1;
    int i = 1;
    cout << "while循环:" << endl;
    while (i < 6)
    {
        pro *= i;
        i += 1;
    }
    cout << "5的阶乘:" << pro << endl;

    // if语句
    cout << "if语句:" << endl;
    string p_name;
    cout << "输入您的名字:";
    cin >> p_name;
    if (p_name != "郭德纲")
    {
        cout << p_name << "是帅哥!" << endl;
    }

    // switch语句
    int in_num;
    cout << "1+1 等于几?" << endl;
    cin >> in_num;
    switch (in_num)
    {
    case 2:
        cout << "答对了,你真棒!" << endl;
        break;
    default:
        cout << "答错了,下次努力!" << endl;
        break;
    }
}


int num = 1;
std::mutex mtx;
std::condition_variable cond;
 
void fun() {
    while(num <=100 ) {
        std::unique_lock<std::mutex> lock(mtx);
        std::cout << num++ << std::endl;
        cond.notify_one();
        cond.wait(lock);
        lock.unlock();
    }
    std::cout << "end" << std::endl;
    cond.notify_one();
}



void Peasant::threadTest()
{
    std::thread a(fun);
    std::thread b(fun);
    if (a.joinable()) a.join();
    if (b.joinable()) b.join();
    return;
}

Peasant::~Peasant()
{
    if (this->_pesticide == nullptr)
    {
        return;
    }
    delete this->_pesticide;
    this->_pesticide = nullptr;
}